import { isTargetInViewport } from '../helper'

const useNavigator = () => {
  /**
   * 滚动到指定目标
   * @param domId 目标id
   * @param containerId 父元素id
   */
  const animateTo = (domId: string, containerId: string, behavior?: ScrollBehavior) => {
    const target = document.getElementById(domId)
    const container = document.getElementById(containerId)
    if (!(target && container)) return
    if (isTargetInViewport(target, container)) return
    container?.scrollTo({ left: target?.offsetLeft - 16, behavior: behavior ?? 'smooth' })
  }

  const animateToOffset = (params: {
    left?: number
    top?: number
    containerId: string
    behavior?: ScrollBehavior
  }) => {
    const { containerId, left, top, behavior } = params
    const container = document.getElementById(containerId)
    if (!container) return
    container.scrollTo({ left, top, behavior: behavior ?? 'smooth' })
  }

  return { animateTo, animateToOffset }
}

export default useNavigator
